import { saveAs } from 'file-saver';

export default function exportFile (config) {
  const defaultConfig = {
    // 接口地址
    api: () => {},
    // 接口入参
    params: {},
    // 文件名
    fileName: '',
    // url导出模式：pdf地址
    url: ''
  };
  config = { ...defaultConfig, ...config };

  const resType = {
    type: 'application/octet-stream'
  };
  if (config.url) {
    // 通过pdf地址导出
    axios({
      url: config.url,
      method: 'get', // 默认值
      responseType: 'blob'
    }).then(res => {
      saveAs(new Blob([res.data], { type: 'application/pdf' }), config.fileName);
    });
  } else {
    // 通过api导出
    config.api(
      config.params,
      {},
      {
        responseType: 'blob'
      }
    ).then(res => {
      saveAs(new Blob([res], resType), config.fileName);
    });
  }
}
